package app

import (
	// "fmt"
	// "time"

	"github.com/go-apibox/api"
	"github.com/go-apibox/filter"
)

func ActionConfigList(c *api.Context) interface{} {
	params := c.NewParams()
	params.AddPagination().AddOrderBy("CreateTime", "desc", []string{"CreateTime"}, false)
	params.Add("ConfigId", filter.EmptyToNil(), filter.Uint32())
	params.Add("Name", filter.EmptyToNil(), filter.String())
	params.Add("Desc", filter.EmptyToNil(), filter.String())
	params.Add("IsDefault", filter.EmptyToNil(), filter.String())
	if apiErr := params.Parse(); apiErr != nil {
		return apiErr
	}

	// session, err := c.Session("default")
	// if err != nil {
	// 	return c.Error.New(api.ErrorInternalError, "SessionFailed").SetMessage(err.Error())
	// }

	list := make([]*TaskConfig, 0, 10)
	return api.List(c, &list, params, map[string]string{
		"Name": "like",
		"Desc": "like",
	})
}

func ActionConfigCreate(c *api.Context) interface{} {
	params := c.NewParams()
	params.Add("Name", filter.EmptyToNil(), filter.Required(), filter.String())
	params.Add("Config", filter.EmptyToNil(), filter.Required(), filter.String())
	params.Add("Desc", filter.EmptyToNil(), filter.String())
	params.Add("IsDefault", filter.EmptyToNil(), filter.Default(IS_N), filter.String())
	if apiErr := params.Parse(); apiErr != nil {
		return apiErr
	}

	return api.Create(c, new(TaskConfig), params)
}

func ActionConfigUpdate(c *api.Context) interface{} {
	params := c.NewParams()
	params.Add("ConfigId", filter.EmptyToNil(), filter.Required(), filter.Uint32())
	params.Add("Name", filter.EmptyToNil(), filter.String())
	params.Add("Config", filter.EmptyToNil(), filter.String())
	params.Add("Desc", filter.EmptyToNil(), filter.String())
	if apiErr := params.Parse(); apiErr != nil {
		return apiErr
	}
	return api.Update(c, new(TaskConfig), params)
}

func ActionConfigDetail(c *api.Context) interface{} {
	params := c.NewParams()
	params.Add("ConfigId", filter.EmptyToNil(), filter.Required(), filter.Uint32())
	if apiErr := params.Parse(); apiErr != nil {
		return apiErr
	}
	return api.Detail(c, new(TaskConfig), params)
}

func ActionConfigGetDefault(c *api.Context) interface{} {
	config := new(TaskConfig)
	has, err := app.engine.Where("is_default=?", IS_Y).Get(config)
	if err != nil {
		app.logger.Error(err)
		return c.Error.New(api.ErrorInternalError, "Connect db faield")
	}
	if !has {
		return c.Error.New(api.ErrorInternalError, "defalut config not exist")
	}

	return config
}

func ActionConfigSetDefault(c *api.Context) interface{} {
	params := c.NewParams()
	params.Add("ConfigId", filter.EmptyToNil(), filter.Required(), filter.Uint32())
	if apiErr := params.Parse(); apiErr != nil {
		return apiErr
	}

	id := params.GetUint32("ConfigId")

	dbSession := app.engine.NewSession()
	if err := dbSession.Begin(); err != nil {
		app.logger.Error(err)
		return c.Error.New(api.ErrorInternalError, "Connect db faield")
	}

	if _, err := dbSession.Where("is_default=?", IS_Y).Cols("is_default").Update(&TaskConfig{IsDefault: IS_N}); err != nil {
		dbSession.Rollback()
		app.logger.Error(err)
		return c.Error.New(api.ErrorInternalError, "Connect db faield")
	}

	if _, err := dbSession.Where("config_id=?", id).Cols("is_default").Update(&TaskConfig{IsDefault: IS_Y}); err != nil {
		dbSession.Rollback()
		app.logger.Error(err)
		return c.Error.New(api.ErrorInternalError, "Connect db faield")
	}

	if err := dbSession.Commit(); err != nil {
		app.logger.Error(err)
		return c.Error.New(api.ErrorInternalError, "Connect db faield")
	}
	return id
}

func ActionConfigDelete(c *api.Context) interface{} {
	params := c.NewParams()
	params.Add("ConfigId", filter.EmptyToNil(), filter.Required(), filter.Uint32())
	if apiErr := params.Parse(); apiErr != nil {
		return apiErr
	}
	params.Set("IsDefault", IS_N)
	return api.Delete(c, new(TaskConfig), params)
}
